package edu.umuc.cmsc495_final_project;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.JButton;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class newTable extends application implements WindowFocusListener {

	private JLabel lblName;
	private JTextField txtFldName;
	private JTextField txtFldQuant;
	private JLabel lblTableType;
	private JLabel lblTableQuant;
	private JComboBox cmbTableType;
	public JButton btnMakeTable;
	private JButton btnCancelTable;

	private int tableID;
	private int xCoordinate;
	private int yCoordinate;
	private int tableQuant;
	private TableType tableType;

	/**
	 * Create the frame.
	 */
	public newTable() {
		
		System.out.println("new table pane");
		openWindow();
		
	}

	private void openWindow() {
		
		final JFrame frame = new JFrame();

		frame.setTitle("New Table");
		frame.setVisible(true);
		frame.setBounds(100, 100, 450, 300);
		// new centerScreen(this);
		frame.getContentPane().setLayout(new GridLayout(3, 2));

		// table type
		lblTableType = new JLabel("Table or Chair Type");
		frame.getContentPane().add(lblTableType);

		cmbTableType = new JComboBox(TableType.values());
		cmbTableType.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				switch (cmbTableType.getSelectedIndex()) {
				case 0: { // bar stool
					tableType = TableType.BAR_STOOL;
					btnMakeTable.setText("Add Stool");
					break;
				}
				case 1: { // two person round
					tableType = TableType.TWO_PERSON_ROUND;
					btnMakeTable.setText("Add Table");
					break;
				}
				case 2: { // 4 person round
					tableType = TableType.FOUR_PERSON_ROUND;
					btnMakeTable.setText("Add Table");
					break;
				}
				case 3: { // 6 person round
					tableType = TableType.SIX_PERSON_ROUND;
					btnMakeTable.setText("Add Table");
					break;
				}
				case 4: { // 2 person rect
					tableType = TableType.TWO_PERSON_RECT;
					btnMakeTable.setText("Add Table");
					break;
				}
				case 5: { // 4 person rect
					tableType = TableType.FOUR_PERSON_RECT;
					btnMakeTable.setText("Add Table");
					break;
				}
				case 6: { // 6 person rect
					tableType = TableType.SIX_PERSON_RECT;
					btnMakeTable.setText("Add Table");
					break;
				}
				case 7: { // 8 person rect
					tableType = TableType.EIGHT_PERSON_RECT;
					btnMakeTable.setText("Add Table");
					break;
				}
				}
			}
		});
		frame.getContentPane().add(cmbTableType);
		
		// table type
				lblTableQuant = new JLabel("Add x amount");
				frame.getContentPane().add(lblTableQuant);
				
				txtFldQuant = new JTextField();
				txtFldQuant.addFocusListener(new FocusAdapter() {
					@Override
					public void focusLost(FocusEvent arg0) {
						tableQuant = Integer.parseInt( txtFldQuant.getText() );
					}
				});
				frame.getContentPane().add(txtFldQuant);
				txtFldQuant.setColumns(10);
				
		// create new table
		btnMakeTable = new JButton("Add Table");
		frame.getContentPane().add(btnMakeTable);

		//
		btnCancelTable = new JButton("CANCEL Table");
		btnCancelTable.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
			}
		});
		frame.getContentPane().add(btnCancelTable);

		frame.addWindowFocusListener(this);
	}

	protected int generateTableID(){
		int tempID = 0;
		Table temp;

		// look through all employees for the highes existing empID
		for (int i = 0; i < db.getTables().size(); i++) {
			temp = db.getTables().get(i);

			try {
				int tempInt = temp.getTableID();
				if (tempInt > tempID)
					tempID = tempInt;
				
			} catch (NumberFormatException nfe) {
				System.err.println("error with generating table id");
			}
		}
		return tempID;
	}
	
	protected int generateStoolID(){
		int tempID = 0;
		Table temp;

		// look through all employees for the highes existing empID
		for (int i = 0; i < db.getBarStools().size(); i++) {
			temp = db.getBarStools().get(i);

			try {
				int tempInt = temp.getTableID();
				if (tempInt > tempID)
					tempID = tempInt;
				
			} catch (NumberFormatException nfe) {
				System.err.println("error with generating table id");
			}
		}
		return tempID;
	}
	
	protected Table getTable() {
		if(tableType == TableType.BAR_STOOL)
			return new Table(generateStoolID(), tableType, DinnerEvent.AVAILABLE);
		else
			return new Table(generateTableID(), tableType, DinnerEvent.AVAILABLE);
	}
	
	public int makeThisMany(){
		return tableQuant;
	}

	protected void close() {
		frame.dispose();
	}

	@Override
	public void windowGainedFocus(WindowEvent arg0) {
	}

	@Override
	public void windowLostFocus(WindowEvent arg0) {
		frame.dispose();
	}
}
